package com.fanghuyun.iswaf.common;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class JspInfo {
	
	private String getRequestFileRealPath(HttpServletRequest request){
		String webRoot = request.getSession().getServletContext().getRealPath("").replaceAll("\\\\", "/"),
			   context = request.getContextPath(), 
			   uri = request.getRequestURI();
		if(context!=null&&webRoot.endsWith(context)){
			return webRoot.substring(0,webRoot.indexOf(context))+uri;
		}
		return webRoot+uri;
	}

	public String getJspInfo(HttpServletRequest request){
		
		StringBuilder out = new StringBuilder();
		out.append("<html>"+"\n"+
		"<head>"+"\n"+
		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"+"\n"+
		"<title>IsWaf info</title>"+"\n"+
		"<style>"+"\n"+
			"body {background-color: #ffffff;color: #000000;}"+"\n"+
			"body,td,th,h1,h2 {font-family: sans-serif;}"+"\n"+
			"pre{margin: 0px;font-family: monospace;}"+"\n"+
			"a:link {color: #000099;text-decoration: none;background-color: #ffffff;}"+"\n"+
			"a:hover {text-decoration: underline;}"+"\n"+
			"table {border-collapse: collapse;margin-bottom: 15px;}"+"\n"+
			".center {text-align: center;}"+"\n"+
			".center table {margin-left: auto;margin-right: auto;text-align: left;}"+"\n"+
			".center th {text-align: center !important;}"+"\n"+
			"td,th {border: 1px solid #000000;font-size: 75%;vertical-align: baseline;}"+"\n"+
			"h1 {font-size: 150%;}"+"\n"+
			"h2 {font-size: 125%;}"+"\n"+
			".p {text-align: left;}"+"\n"+
			".e {background-color: #ccccff;font-weight: bold;color: #000000;width: 600px;}"+"\n"+
			".h {background-color: #9999cc;font-weight: bold;color: #000000;}"+"\n"+
			".v {background-color: #cccccc;color: #000000;}"+"\n"+
			".vr {background-color: #cccccc;text-align: right;color: #000000;}"+"\n"+
			"img {float: right;border: 0px;}"+"\n"+
			"hr {width: 600px;background-color: #cccccc;border: 0px;height: 1px;color: #000000;}"+"\n"+
		"</style>"+"\n"+
		"</head>"+"\n"+
		"<body>"+"\n"+
			"<div class=\"center\">"+"\n"+
				"<table border=\"0\" cellpadding=\"3\" width=\"600\">"+"\n"+
					"<tbody>"+"\n"+
						"<tr class=\"h\">"+"\n"+
							"<td>"+"\n"+
								"<a href=\"http://javaweb.org/\"><img border=\"0\" src=\"\" alt=\"Java Logo\">"+"\n"+
								"</a>"+"\n"+
								"<h1 class=\"p\">"+System.getProperty("java.runtime.name")+":"+System.getProperty("java.runtime.version")+"</h1>"+"\n"+
							"</td>"+"\n"+
						"</tr>"+"\n"+
					"</tbody>"+"\n"+
				"</table>"+"\n"+
				"<br/>"+"\n"+
			
				"<h2>System Environment Variables</h2>"+"\n"+
				"<table border=\"0\" width=\"600\">"+"\n");
				
							Map<String,String> map = System.getenv();
							for(String s:map.keySet()){
								out.append("<tr>"+"\n"+
									"<td class=\"e\">"+s+"</td>"+"\n"+
									"<td class=\"v\">"+map.get(s)+"</td>"+"\n"+
								"</tr>"+"\n");
							}
				out.append("</table>"+"\n"+
				"<h2>"+request.getSession().getServletContext().getServerInfo()+"Environment</h2>"+"\n"+
				"<table width=\"600\">"+"\n"+
						"<tr>"+"\n"+
							"<td class=\"e\">SERVER_NAME</td>"+"\n"+
							"<td class=\"v\">"+request.getServerName()+"</td>"+"\n"+
						"</tr>"+"\n"+
						"<tr>"+"\n"+
							"<td class=\"e\">SERVER_ADDR</td>"+"\n"+
							"<td class=\"v\">"+request.getLocalAddr()+"</td>"+"\n"+
						"</tr>"+"\n"+
						"<tr>"+"\n"+
							"<td class=\"e\">SERVER_PORT</td>"+"\n"+
							"<td class=\"v\">"+request.getLocalPort()+"</td>"+"\n"+
						"</tr>"+"\n"+
						"<tr>"+"\n"+
							"<td class=\"e\">DOCUMENT_ROOT</td>"+"\n"+
							"<td class=\"v\">"+request.getSession().getServletContext().getRealPath("/").replaceAll("\\\\","/")+"</td>"+"\n"+
						"</tr>"+"\n"+
						"<tr>"+"\n"+
							"<td class=\"e\">SCRIPT_FILENAME</td>"+"\n"+
							"<td class=\"v\">"+getRequestFileRealPath(request)+"</td>"+"\n"+
						"</tr>"+"\n");
							Enumeration<String> e = request.getHeaderNames();
							while(e.hasMoreElements()){
								String k = (String)e.nextElement();
								out.append("<tr>"+"\n"+
								"<td class=\"e\">"+k+"</td>"+"\n"+
								"<td class=\"v\">"+request.getHeader(k)+"</td>"+"\n"+
								"</tr>"+"\n");
							}
				out.append("</table>"+"\n"+
				"<h2>System Properties</h2>"+"\n"+
				"<table border=\"0\" width=\"600\">"+"\n");
							for(Object s:System.getProperties().keySet()){
								out.append("<tr>"+"\n"+
									"<td class=\"e\">"+s+"</td>"+"\n"+
									"<td class=\"v\">"+System.getProperty((String)s)+"</td>"+"\n"+
								"</tr>"+"\n");
							}
				out.append("</table>"+"\n"+
				"<br/>"+"\n"+
				
				"<h2>Java License</h2>"+"\n"+
				"<table border=\"0\" cellpadding=\"3\" width=\"600\">"+"\n"+
					"<tbody>"+"\n"+
						"<tr class=\"v\">"+"\n"+
							"<td>"+"\n"+
								"<p>"+"\n"+
									"javaweb.org&p2j.cn"+"\n"+
								"</p>"+"\n"+
							"</td>"+"\n"+
						"</tr>"+"\n"+
					"</tbody>"+"\n"+
				"</table>"+"\n"+
				"<br/>"+"\n"+
			"</div>"+"\n"+
		"</body>"+"\n"+
		"</html>");
		return out.toString();
	}
	
}
